<?php 
	include './init.php';
	$a=$_GET['a'];
	$add_model=new Model('address');

	switch ($a) {
		case 'add':
			//判断数据是否为空
			if (!empty($_POST)) 
			{
				foreach ($_POST as $key => $value) 
				{
					if (empty($value)) 
					{
						error('表单不能为空!');
						return false;
					}
				}
			}

			$data['uid']=$_SESSION['user']['id'];
			$data['address']=$_POST['address'];
			$data['receiver']=$_POST['receiver'];
			$data['tel']=$_POST['tel'];
			$data['postcode']=$_POST['postcode'];

			$res=$add_model->insert($data);
			if ($res) 
			{
				success('添加地址成功',URL.'/order.php',1);
				return false;
			}
			break;
		
		case 'choose':
			//判断是否选择地址
			if (empty($_POST['address'])) 
			{
				error('请选择收货地址！');
				return false;
			}
			
			//接受地址id
			$add_id=$_POST['address'];
			//开始添加数据库
			$list_model=new Model('order_list');
			$info_model=new Model('order_info');

			$data['ordernumber']='mz'.date('Ymd',time()).uniqid().rand(0,9999);
			$data['ordertime']=time();
			$data['add_id']=$add_id;
			$data['uid']=$_SESSION['user']['id'];

			//插入数据库
			$order_id=$list_model->insert($data);

			//循环将商品插入到订单详情表
			foreach ($_SESSION['car'] as $k => $v) 
			{
				$dat['goods_id']=$v['gid'];
				$dat['order_id']=$order_id;
				$dat['num']=$v['num'];
				$result=$info_model->insert($dat);
			}
			if ($result) 
			{
				unset($_SESSION['car']);
				header('location:'.URL.'/confirm.php');
			}
			break;
	}
?>